<?php

namespace App\Models;


class OnlineWaterBill extends BaseModel
{
    protected $table = "waterbill_order";

    protected $rules = [
        'order_sn' => 'required',
        'admin_id' => 'required',
        'shop_id' => 'required',
        'mid' => 'required',
        'waterbill_id' => 'required',
        'goods_id' => 'required',
        'goods_name' => 'required',
        'goods_price' => ['required', 'numeric', 'regex:/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])/'],
        'order_amount' => ['required', 'numeric', 'regex:/^([1-9]\d*(\.\d*[1-9])?)|(0\.\d*[1-9])/'],
        'title' => 'required',
        'buy_num' => 'required|numeric',
        'gift_num' => 'required|numeric',
        'total_num' => 'required|numeric',
        'rest_num' => 'required|numeric|same:total_num',
    ];

    protected $message = [
        'order_sn.required' => '订单号获取失败',
        'admin_id.required' => '商户ID获取失败',
        'shop_id.required' => '店铺ID获取失败',
        'mid.required' => '用户ID获取失败',
        'waterbill_id.required' => '水票ID获取失败',
        'goods_id.required' => '商品ID获取失败',
        'goods_name.required' => '商品名称获取失败',
        'goods_price.required' => '商品价格获取失败',
        'goods_price.numeric' => '商品价格必须是纯数字',
        'goods_price.regex' => '商品价格不能小于0',
        'order_amount.required' => '订单总价格获取失败',
        'order_amount.numeric' => '订单总价格必须是纯数字',
        'order_amount.regex' => '订单总价格不能小于0',
        'title.required' => '水票套餐信息获取失败',
        'buy_num.required' => '购买数量获取失败',
        'buy_num.numeric' => '购买数量必须是纯数字',
        'gift_num.required' => '赠送数量获取失败',
        'gift_num.numeric' => '赠送数量必须是纯数字',
        'total_num.required' => '总数量获取失败',
        'total_num.numeric' => '总数量必须是纯数字',
        'rest_num.required' => '剩余总数量获取失败',
        'rest_num.numeric' => '剩余总数量必须是纯数字',
        'rest_num.same' => '总数量与剩余总数量不一致',
    ];

    public function waterBill()
    {
        return $this->hasOne('App\Models\WaterBill', 'id', 'waterbill_id');
    }


    public function getListByMid($mid)
    {
        $where['mid'] = $mid;
        $where['status'] = 1;
        $fields = 'id,mid,goods_id,goods_name,rest_num,admin_id';
        $rows = $this->_getList($where, $fields)->toArray();
        return $rows;
    }


    public function checkData($ids)
    {
        if (!$ids) {
            $this->error = '参数错误';
            return false;
        }

        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $where['goods_id'] = ['in', $ids];
        $where['shop_id'] = request('shop_id');
        $res = $this->_getList($where);
        if ($res) {
            return false;
        }
        return true;
    }

    public function addBill()
    {
        $flag = session('flag_bill');
        if ($flag) {
            if (time() - $flag > 2) {
                $this->error = '请勿重复提交订单！';
                return false;
            }
        }
        if (!(request('mid') && request('aid') && request('sid') && request('waterbill_id') && request('bill_id'))) {
            $this->error = '非法操作！';
            return false;
        }
        $data['order_sn'] = makeOrderSn(request('mid'));
        $data['admin_id'] = request('aid');
        $data['shop_id'] = request('sid');
        $data['mid'] = request('mid');
        $data['type'] = request('type');
        $data['waterbill_id'] = request('waterbill_id');
        $waterBill = new WaterBill();
        $info = $waterBill->_getOneById($data['waterbill_id']);
        foreach ($info['waterbill_category'] as $v) {
            if ($v['id'] == request('bill_id')) {
                $data['title'] = $v['title'];
                $data['buy_num'] = $v['buy_num'];
                $data['gift_num'] = $v['gift_num'];
                $data['total_num'] = $v['total_num'];
                $data['description'] = $v['description'];
            }
        }
        $data['category_id'] = $info['category_id'];
        $data['brand_id'] = $info['brand_id'];
        $data['bucket_category_id'] = $info['bucket_category_id'];
        $data['shop_goods_id'] = $info['shop_goods_id'];
        $data['goods_id'] = $info['goods_id'];
        $data['goods_name'] = $info['goods_name'];
        $data['goods_price'] = $info['goods_price'];
        $data['order_amount'] = $data['buy_num']*$info['goods_price'];
        $data['rest_num'] = $data['total_num'];
        $data['addtime'] = time();
        if (!$this->_validate($data)) {
            return false;
        }
        $res = $this->_save($data);
        if (!$res) {
            $this->error = '保存水票信息失败！';
            return false;
        }
        return $data;
    }

    public function dealOrder($order_sn)
    {
        //修改数据库订单状态
        $order = $this->_getOne(['order_sn' => $order_sn]);
        if (!$order) {
            $this->error = '订单号不存在';
            return false;
        }
        $order = $order->toArray();
        if ($order['pay_status'] == 10) {
            $data['id'] = $order['id'];
            $data['pay_status'] = 20;
            $data['payment_time'] = time();
            $this->_start();
            try {
                $a = $this->_save($data);
                tolog('a----' . $a);
                //更新水票销量
                $waterBill = new WaterBill();
                $b = $waterBill->_inc(['id' => $order['waterbill_id']], 'sale_num');
                tolog('b----' . $b);
                //更新店铺营业额
                $shop = new Shop();
                $shopData = [
                    'total_water_bill_num' => "`total_water_bill_num`+1",
                    'sale_money' => "`sale_money`+{$order['order_amount']}",
                ];
                $c = $shop->_updateOrigin(['id' => $order['shop_id']], $shopData);
                tolog('c----' . $c);
                $this->_commit();
                return true;
            } catch (\Exception $e) {
                $this->_rollBack();
                return false;
            }
        }
    }


    public function importBill($memberInfo,$shopId,$waterbill_id,$rest_num)
    {
        $data['order_sn'] = makeOrderSn($memberInfo['id']);
        $data['admin_id'] = $memberInfo['admin_id'];
        $data['shop_id'] = $shopId;
        $data['mid'] = $memberInfo['id'];
        $data['type'] = 1;
        $data['pay_status'] = 20;
        $data['waterbill_id'] = $waterbill_id;
        $waterBill = new WaterBill();
        $info = $waterBill->_getOneById($data['waterbill_id']);
        $data['title'] = $info['waterbill_category'][0]['title'];
        $data['buy_num'] = $info['waterbill_category'][0]['buy_num'];
        $data['gift_num'] = $info['waterbill_category'][0]['gift_num'];
        $data['total_num'] = $rest_num;
        $data['description'] = $info['waterbill_category'][0]['description'];
        $data['category_id'] = $info['category_id'];
        $data['brand_id'] = $info['brand_id'];
        $data['bucket_category_id'] = $info['bucket_category_id'];
        $data['shop_goods_id'] = $info['shop_goods_id'];
        $data['goods_id'] = $info['goods_id'];
        $data['goods_name'] = $info['goods_name'];
        $data['goods_price'] = $info['goods_price'];
        $data['order_amount'] = $data['buy_num']*$info['goods_price'];
        $data['rest_num'] = $rest_num;
        $data['addtime'] = time();
        if (!$this->_validate($data)) {
            return false;
        }
        return $this->_save($data);
    }
}
